<?php

namespace App\View\Components;

use App\Models\Admin;
use App\Models\FilmTypeMaster;
use App\Models\Warranty;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\Component;

class DDChainSelect extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        $items = $this->items();
        return view('components.d-d-chain-select', [
            'items' => $items,
        ]);
    }

    public function items()
    {
        $user_type = session('admin.user_type');
        $user_id = session('admin.id');
        $model = Admin::where('status', 1)
            ->where('user_type', 2);

        if ($user_type == 4) {
            $users = DB::table('users_relation')
                ->where('user_id', $user_id)
                ->get();
            if (empty($users)) {
                return [];
            }
            $userIdArr = [];
            foreach ($users as $user) {
                $userIdArr[] = $user->belong_user_id;
            }
            $model->whereIn('id', $userIdArr);
        }

        return $model
            ->orderBy('status')
            ->orderBy('unique_id')
            ->select(['id', 'abbr'])
            ->get();
    }
}
